<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Admin;
use app\admin\model\AdminRole;
use app\admin\model\Item;
use app\admin\model\Market;
use app\admin\model\MarketAction;
use app\admin\model\Mobile;
use app\Request;
use think\Config;
use think\db\Query;
use think\facade\Db;
use think\facade\Validate;

/*
 * 用戶管理-管理员
 */
class AdminController extends BaseController
{

    /**管理员列表
     * @fun:
     *
     * @param Request $request
     * @return \think\response\Json
     * @author:崔彦雷
     * @time:2022/10/21 下午 3:33
     */
    public function adminList(Request $request){

        $param = $request->param();

        $info = Admin::where('a.status','in',[1,2])
            /*->with(['mobiles'])
            ->with(['mobiles'=>function(Query $query) use ($middle_type_ids){
                $query->where('middle_type_id','in',$middle_type_ids);
            }])*/
            /*->with(['mobiles'=>function(Query $query){
                $query->where('table_type',1);
            }])*/
            ->where(function (Query $query)use ($param){
                //名称
                if (!empty($param['realname']) && isset($param['realname'])){
                    $query->where('a.realname','like','%' . $param['realname'] . '%');
                }
                //手机号
                if (!empty($param['mobile']) && isset($param['mobile'])){
                    $query->where('a.mobile','like','%' . $param['mobile'] . '%');
                }
                //性别
                if (!empty($param['sex']) && isset($param['sex'])){
                    $query->where('a.sex', $param['sex']);
                }
                //部门
                /*if (!empty($param['organization_id']) && isset($param['organization_id'])){
                    $query->where('a.organization_id', $param['organization_id']);
                }*/
            })
            ->alias('a')
            ->field(['a.*','ar.role role_name'])
            //->leftJoin('organization o','a.organization_id = o.id')
            ->leftJoin('admin_role ar','a.role_id = ar.id')
            //->leftJoin('mobile m','a.id = m.table_id and m.table_type = 1')
            ->order('a.id desc')
            ->group('a.id')
            ->paginate($param['perPage']);
        return $this->suc_json('',$info);

    }


    /**重置管理员密码；
     * @fun:
     *
     * @author:崔彦雷
     * @time:2022/10/21 下午 2:11
     */
    public function editAdminPwd(Request $request){

        $id = $request->param('id');
        if (empty($id)){
            return $this->err_json('id不能为空');
        }

        $salt = randStr(10);

        $admin = Admin::where('id', $id)->find();
        $admin->password = md5(config('adminconfig.DefaultPwd').$salt);
        $admin->salt = $salt;
        $admin->update_time = date('Y-m-d H:i:s',time());
        $info = $admin->save();

        if ($info == 1){
            return $this->suc_json('密码重置成功', 'default');
        }else{
            return $this->err_json('密码重置失败');
        }

    }


    /**删除管理员
     * @fun:
     *
     * @author:崔彦雷
     * @time:2022/10/21 下午 2:11
     */
    public function delAdmin(Request $request){

        $id = $request->param('id');
        if (empty($id)){
            return $this->err_json('id不能为空');
        }
        $admin = Admin::where('id', $id)->find();
        $admin->status = 3;
        $admin->update_time = date('Y-m-d H:i:s',time());
        $info = $admin->save();

        if ($info == 1){
            return $this->suc_json('删除成功', 'default');
        }else{
            return $this->err_json('删除失败');
        }

    }


    /**启用禁用管理员
     * @fun:
     *
     * @author:崔彦雷
     * @time:2023/4/17 下午 5:20
     */
    public function editStatus(Request $request){

        $id = $request->param('id');
        if (empty($id)){
            return $this->err_json('id不能为空');
        }
        $admin = Admin::where('id', $id)->find();
        if ($admin['status'] == 3){
            return $this->err_json('该人员已删除');
        }
        if ($admin['status'] == 1){
            $admin->status = 2;
            $msg = '禁用';
        }else{
            $admin->status = 1;
            $msg = '启用';
        }

        $admin->update_time = date('Y-m-d H:i:s',time());
        $info = $admin->save();

        if ($info == 1){
            return $this->suc_json("$msg . 成功");
        }else{
            return $this->err_json("$msg . 失败");
        }

    }


    /**编辑管理员
     * @fun:
     *
     * @return \think\response\Json|void
     * @author:崔彦雷
     * @time:2022/10/20 下午 5:36
     */
    public function editAdmin(Request $request){

        $param = $request->param();
        $type = $param['type'];//1编辑前回显数据  2编辑提交
        if($type == 1){
            $admin = Admin::where('id',$param['id'])->find();
            return $this->suc_json('查询成功',$admin);
        }  else {
            $validate = Validate::rule([
                'id|id' => 'require|number',
                'username|登录账号' => 'require',
                'role_id|角色id' => 'require',
                'is_super|是否为超级管理员' => 'require|in:1,2',
            ]);
            if (!$validate->check($param)){
                return $this->err_json($validate->getError());
            }

            if (empty($param['mobile'])){
                return $this->err_json('手机号不能为空');
            }else{
                    $isMob="/^1[3-8]{1}[0-9]{9}$/";
                    if(!preg_match($isMob,$param['mobile'])){
                        return $this->err_json('请输入正确的手机号');
                    }
            }

            $admin = Admin::where('id',$param['id'])->find();
            $admin->username = $param['username'];
            $admin->realname = isset($param['realname']) ? $param['realname'] : '';
            $admin->mobile = isset($param['mobile']) ? $param['mobile'] : '';
            $admin->sex = isset($param['sex']) ? $param['sex'] : 3;
            $admin->avatar = isset($param['avatar']) ? $param['avatar'] : '';
            $admin->job = isset($param['job']) ? $param['job'] : '';
            $admin->is_super = $param['is_super'];
            $admin->role_id = $param['role_id'];
            $info = $admin->save();

            if ($info == 1){
                return $this->suc_json('编辑成功','default');
            }else{
                return $this->err_json('编辑失败');
            }
        }

    }


    /**添加管理员
     * @fun:
     *
     * @return \think\response\Json|void
     * @author:崔彦雷
     * @time:2022/10/20 下午 5:36
     */
    public function addAdmin(Request $request){

        $param = $request->param();
        $validate = Validate::rule([
            'username|登录账号' => 'require',
            'role_id|角色id' => 'require',
            'is_super|是否为超级管理员' => 'require|in:1,2',
        ]);
        if (!$validate->check($param)){
            return $this->err_json($validate->getError());
        }

        if (empty($param['mobile'])){
            return $this->err_json('手机号不能为空');
        }else{
            $isMob="/^1[3-8]{1}[0-9]{9}$/";
            if(!preg_match($isMob,$param['mobile'])){
                return $this->err_json('请输入正确的手机号');
            }
        }
        $salt = randStr(10);

        $admin = new Admin();
        $admin->username = $param['username'];
        $admin->password = md5(config('adminconfig.DefaultPwd').$salt);
        $admin->realname = isset($param['realname']) ? $param['realname'] : '';
        $admin->mobile = isset($param['mobile']) ? $param['mobile'] : '';
        $admin->sex = isset($param['sex']) ? $param['sex'] : 3;
        $admin->avatar = isset($param['avatar']) ? $param['avatar'] : '';
        $admin->job = isset($param['job']) ? $param['job'] : '';
        $admin->salt = $salt;
        $admin->is_super = $param['is_super'];
        $admin->role_id = $param['role_id'];
        $info = $admin->save();

        if ($info == 1){
            return $this->suc_json('添加成功','default');
        }else{
            return $this->err_json('添加失败');
        }

    }

    

}